#include <iostream>

#include "Terrain.h"

using namespace std;


/**
 * Constructor
 * 
 */
Terrain::Terrain()
{
	m_strName = "";
}


/**
 * Destructor
 * Encounter Table is emptied
 */
Terrain::~Terrain()
{
	for (unsigned int i = 0; i < m_encounterTable.size(); ++i)
	{
		t_encounter* encounterToDelete = m_encounterTable[i];
		delete encounterToDelete;
		m_encounterTable.erase(m_encounterTable.begin() + i);
	}
}


/**
 * Set name of the Terrain
 * @param std::string newName
 * @return void
 */
void Terrain::setName(std::string newName)
{
	m_strName = newName;
}


/**
 * Get the name of the Terrain
 * @param  void
 * @return std::string
 */
std::string Terrain::getName()
{
	return m_strName;
}


/**
 * Add a random encounter in the encounter table
 * @param int rollLow
 * @param int rollHigh
 * @param char* npcName
 * @return void
 */
void Terrain::addRandomEncounter(int rollLow, int rollHigh, char* npcName)
{
	t_encounter* newEncounter = new t_encounter;

	newEncounter->m_roll.mLow = rollLow;
	newEncounter->m_roll.mHigh = rollHigh;
	newEncounter->m_strMonsterName = npcName;
	m_encounterTable.push_back(newEncounter);
}

/**
 * Print information about the Terrain
 * @param void
 * @return void
 */
void Terrain::print()
{
	cout << "#####" << endl;
	cout << "Terrain name: " << m_strName << endl;
	cout << "Encounter table" << endl;
	cout << "..............." << endl;
	for (unsigned int i = 0; i < m_encounterTable.size(); ++i)
	{
		cout << m_encounterTable[i]->m_roll.mLow << "-"
			 << m_encounterTable[i]->m_roll.mHigh << "\t\t"
			 << m_encounterTable[i]->m_strMonsterName << endl;
	}
	cout << "#####" << endl;
}


/**
 * Check the Encounter table for an NPC
 * @param void
 * @return std::string - result of the check
 */
std::string  Terrain::getRandomEncounter()
{
	int roll = Random(1, 100);
	std::string strResult = "";

	for (unsigned int i = 0; i < m_encounterTable.size(); ++i)
	{
		if ((roll > m_encounterTable[i]->m_roll.mLow) &&
			(roll < m_encounterTable[i]->m_roll.mHigh))
		{
			strResult = m_encounterTable[i]->m_strMonsterName;
		}
	}
	return strResult;
}
